package com.tybbank.Login;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tybbank.Base.BaseActivity;
import com.tybbank.Bean.ForgetBean;
import com.tybbank.Bean.YZMBean;
import com.tybbank.R;
import com.tybbank.Utils.Constant;
import com.tybbank.Utils.CountDownButtonHelper;
import com.tybbank.Utils.MD5Utils;
import com.tybbank.Utils.MyDialogUtils;
import com.tybbank.Utils.YanZhengUtil;
import com.tybbank.Utils.logUtilssss;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2018/9/26 0026.
 */

public class FindPasswordActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.et_yzm)
    EditText etYzm;
    @BindView(R.id.bt_yanzhengma)
    Button btYanzhengma;
    @BindView(R.id.bt_enter)
    Button btEnter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_findpasw);
        ButterKnife.bind(this);
        toolbar(toolbar, getString(R.string.wangjimima), R.mipmap.icon_close);
    }

    @Override
    public void LeftClose() {
        super.LeftClose();
        FindPasswordActivity.this.finish();
    }

    @OnClick({R.id.bt_yanzhengma, R.id.bt_enter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_yanzhengma:
                if (etPhone.getText().toString().trim().length() == 0) {
                    Toast.makeText(getBaseContext(),  getString(R.string.phone), Toast.LENGTH_SHORT).show();
                    return;
                }  else {
                    String time = MD5Utils.getTime();
                    SortedMap<String, String> parameters = new TreeMap<String, String>(
                            new Comparator<String>() {
                                public int compare(String obj1, String obj2) {
                                    // 降序排序
                                    return obj1.compareTo(obj2);
                                }
                            });
                    parameters.put("phone", etPhone.getText().toString().trim());
                    parameters.put("TimeStamp", time);
                    parameters.put("type", "2");
                    String sign = MD5Utils.createSign(parameters);
                    getokhttp(time, sign);
                }
                break;
            case R.id.bt_enter:
                if (etPhone.getText().toString().trim().length() == 0) {
                    Toast.makeText(getBaseContext(),  getString(R.string.phone), Toast.LENGTH_SHORT).show();
                    return;
                } else if (etPwd.getText().toString().trim().length() == 0) {
                    Toast.makeText(getBaseContext(), getString(R.string.pwd), Toast.LENGTH_SHORT).show();
                    return;
                } else if (etYzm.getText().toString().length() == 0) {
                    logUtilssss.toast(this,getString(R.string.sryzm));
                    return;
                } else {
                    if (YanZhengUtil.isLetterDigit(etPwd.getText().toString().trim()) == true) {
                        inithttp();//接口

                    } else {
                        Toast.makeText(getBaseContext(), getString(R.string.pwdhint), Toast.LENGTH_SHORT).show();
                        return;

                    }


                }

                break;
        }
    }

    private void inithttp() {
        String time = MD5Utils.getTime();
        SortedMap<String, String> parameters = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });
        parameters.put("account", etPhone.getText().toString().trim());
        parameters.put("verify_code", etYzm.getText().toString().trim());
        parameters.put("new_pwd", MD5Utils.md5Password(etPwd.getText().toString().trim()));
        parameters.put("TimeStamp", time);
        String sign = MD5Utils.createSign(parameters);
        final Dialog dialog = MyDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        dialog.show();
        OkHttpUtils
                .post()
                .url(Constant.FOGR_LOGIN)
                .addParams("account", etPhone.getText().toString().trim())
                .addParams("verify_code", etYzm.getText().toString().trim())
                .addParams("new_pwd", MD5Utils.md5Password(etPwd.getText().toString().trim()))
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        Toast.makeText(getBaseContext(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                        return;
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        logUtilssss.e("TAG忘记登录密码"+ response.toString());
                        dialog.dismiss();
                        string(response);
                    }

                    private void string(String json) {
                        try {
                            ForgetBean enterBean = new Gson().fromJson(json, new TypeToken<ForgetBean>() {
                            }.getType());
                            int status = enterBean.getCode();
                            if (status == 1011) {
                                Toast.makeText(getBaseContext(), enterBean.getMsg(), Toast.LENGTH_SHORT).show();
                                FindPasswordActivity.this.finish();
                            } else {
                                Toast.makeText(getBaseContext(), enterBean.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        }catch (Exception e ){

                        }
                    }
                });
    }

    //   短信接口
    private void getokhttp(String time, String sign) {
        final Dialog dialog = MyDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        dialog.show();
        OkHttpUtils
                .post()
                .url(Constant.SMS)
                .addParams("phone", etPhone.getText().toString().trim())
                .addParams("type", "2")
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        Toast.makeText(getBaseContext(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                        return;
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        logUtilssss.e("TAG短信接口"+ response.toString());
                        dialog.dismiss();
                        string(response);
                    }

                    private void string(String json) {
                        YZMBean enterBean = new Gson().fromJson(json, new TypeToken<YZMBean>() {
                        }.getType());
                        int status = enterBean.getCode();
                        if (status == 1011) {
                            //    //短信请求的的代码
                            CountDownButtonHelper helper = new CountDownButtonHelper(btYanzhengma,
                                    getString(R.string.sent), 60, 1);
                            helper.setOnFinishListener(new CountDownButtonHelper.OnFinishListener() {
                                @Override
                                public void finish() {
                                    btYanzhengma.setText(getString(R.string.hqyzm));
                                }
                            });
                            helper.start();
                            Toast.makeText(getBaseContext(), enterBean.getMsg(), Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getBaseContext(), enterBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }


                    }
                });
    }
}
